package cn.toutatis.sandbox.root.security.service.controller;


import cn.toutatis.sandbox.root.systemService.service.SystemDictionaryService;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Properties;


/**
 * 认证页面
 * @author Toutatis_Gc
 */
@RestController
@Transactional(rollbackFor = Exception.class)
@RequestMapping("/auth")
public class AuthorityController {

    @Autowired
    SystemDictionaryService dictionaryService;

    @Autowired
    DefaultKaptcha defaultKaptcha;

    @RequestMapping(value = "/authPage",method = RequestMethod.GET)
    public ModelAndView index(){
        return new ModelAndView("root/Identification");
    }


    /**
     * 允许时间差（毫秒）
     */
    private static final int ALLOW_INTERVAL = 1000;

    public static final String CODE_SESSION_KEY = "CHECK_CODE";

    /**
     * @param request 存储验证码生成的文本
     * @param response 返回验证码的jpg
     * @param t 请求时间戳
     * @throws IOException 生成问题
     */
    @RequestMapping("/generateCheckCode")
    public void getCheckCode(HttpServletRequest request, HttpServletResponse response,long t) throws IOException {
        long current = System.currentTimeMillis();
        if (current - t > ALLOW_INTERVAL){
            return;
        }
        byte[] imgByteArray;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        String createText = defaultKaptcha.createText();
        System.err.println(createText);
        request.getSession().setAttribute(CODE_SESSION_KEY, createText);
        BufferedImage bufferedImage = defaultKaptcha.createImage(createText);
        ImageIO.write(bufferedImage, "JPG", byteArrayOutputStream);
        imgByteArray = byteArrayOutputStream.toByteArray();
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        ServletOutputStream servletOutputStream = response.getOutputStream();
        servletOutputStream.write(imgByteArray);
        servletOutputStream.flush();
        servletOutputStream.close();
    }

    @Bean
    public DefaultKaptcha getKaptchaBean() {
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Properties properties = new Properties();
//        图片边框
        properties.setProperty("kaptcha.border", "no");
//         图片宽
        properties.setProperty("kaptcha.image.width", "100");
//         图片高
        properties.setProperty("kaptcha.image.height", "30");
//         字体大小
        properties.setProperty("kaptcha.textproducer.font.size", "25");
//        波纹效果
        properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.ShadowGimpy");
//         验证码长度
        properties.setProperty("kaptcha.textproducer.char.length", "4");
//          验证码字符集
        properties.setProperty("kaptcha.textproducer.char.string", "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
        Config config = new Config(properties);

        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
}
